package au.com.evoCoach.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import au.com.evoCoach.enums.AFLTeam;
import au.com.evoCoach.enums.PlayerStatus;
import au.com.evoCoach.enums.PlayerType;
import au.com.evoCoach.enums.Position;

@Entity
public class Player
{
	private Integer id;
	private String firstName;
	private String surname;
	private AFLTeam team;
	private Integer playerNumber;
	private PlayerType playerType;
	private PlayerStatus playerStatus;
	private List<PlayerPosition> playerPositions;
	
	@Id
	@GeneratedValue
	@Column
	public Integer getId() {
		return id;
	}
	
	public void setId(final Integer id) {
		this.id = id;
	}

	@Column
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	@Column
	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	@Enumerated(EnumType.STRING)
	public AFLTeam getTeam() {
		return team;
	}

	public void setTeam(AFLTeam team) {
		this.team = team;
	}
	
	@Column(name = "type")
	@Enumerated(EnumType.STRING)
	public PlayerType getPlayerType() {
		return playerType;
	}

	public void setPlayerType(PlayerType playerType) {
		this.playerType = playerType;
	}	
	
	@Column(name = "status")
	@Enumerated(EnumType.STRING)
	public PlayerStatus getPlayerStatus() {
		return playerStatus;
	}

	public void setPlayerStatus(PlayerStatus playerStatus) {
		this.playerStatus = playerStatus;
	}	

	@Column
	public Integer getPlayerNumber() {
		return id;
	}
	
	public void setPlayerNumber(final Integer playerNumber) {
		this.playerNumber = playerNumber;
	}

	@OneToMany(mappedBy = "player", fetch = FetchType.EAGER)
	public List<PlayerPosition> getPlayerPositions() {
		return playerPositions;
	}

	public void setPlayerPositions(List<PlayerPosition> playerPositions) {
		this.playerPositions = playerPositions;
	}
	
	@Transient
	public String getPositions() {
		String result = "";
		boolean isFirst = true;
		for (PlayerPosition position : playerPositions) {
		    if (!position.getPosition().name().equals("N_A")) {
    			if (!isFirst) {
    				result += ", ";
    			}
    			else {
    				isFirst = false;
    			}
    			
    			result += position.getPosition().name();
		    }
		}
		
		return result;
	}
	
	@Transient
	public String getDescription() {
		return firstName + " " + surname + " (" + team.getLabel() + ")";
	}
	
	@Transient
	public String getDescriptionShort() {
		return firstName + " " + surname + " (" + team + ")";
	}	
	
	@Transient
	public String getFullName() {
		return firstName + " " + surname;
	}	
	
	public boolean playsPosition(Position positionVal) {
		for (PlayerPosition position : playerPositions) {
			if (position.getPosition().name().equals(positionVal.name())) {
				return true;
			}
		}
		
		return false;
	}
	
	@Override
	public boolean equals(Object obj) {
	    // TODO Auto-generated method stub
	    return ((Player) obj).getId().equals(getId());
	}
	
}
